/**
   device_controller.h - Created by Timothy Morey on 10/10/2011.
*/

#ifndef __DEVICE_CONTROLLER_H
#define __DEVICE_CONTROLLER_H

#include <pthread.h>

class DeviceController
{

public:
  DeviceController(unsigned char did, unsigned int minPart, 
		   unsigned int maxPart, pthread_barrier_t* barrier);
  ~DeviceController();

public:
  double GetAverageGPUTime() const { return _AverageGPUTime; }
  double GetAverageCPUInnerTime() const { return _AverageCPUInnerTime; }
  double GetAverageCPUOuterTime() const { return _AverageCPUOuterTime; }
  unsigned int GetCurrentStep() const { return _CurStep; }
  unsigned char GetDeviceID() const { return _DeviceID; }

protected:
  static void* ThreadEntryPoint(void* arg);
  void ThreadLoop();

  void Step();
  void InitDevMemory();
  void FreeDevMemory();

  void SyncPartsHostToDevice();
  void SyncPartsDeviceToHost();
  void SyncPosHostToDevice();
  void SyncPosDeviceToHost();

protected:
  unsigned char _DeviceID;
  unsigned int _MinParticleID;
  unsigned int _MaxParticleID;
  unsigned int _CurStep;

  pthread_t _ThreadID;
  pthread_barrier_t* _Barrier;

  DoF* _DevX;
  DoF* _DevY;
  DoF* _DevVX;
  DoF* _DevVY;
  DoF* _DevVM;
  DoF* _DevAX;
  DoF* _DevAY;

  double _AverageGPUTime;
  double _AverageCPUInnerTime;
  double _AverageCPUOuterTime;
  double _GPUElapsedTime;

};

#endif
